<template>
    <div>
        <div class="wrap">
            <el-form ref="loginRef" :rules="rules" :model="userInfo" class="login">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="userInfo.username" clearable @keydown.enter.native="login"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="userInfo.password" clearable show-password @keydown.enter.native="login"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="login" class="btn">登陆</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
import { userlogin } from "../request/api"
export default {
    data() {
        return {
            userInfo: {
                username: "",
                password: ""
            },
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
                ]

            }
        }
    },
    methods: {
        login() {
            //如果输入框没有写东西，判空
            this.$refs.loginRef.validate((val) => {
                // console.log(val);
                if (val) {
                    console.log(this.userInfo);
                    userlogin(this.userInfo).then(res => {
                        console.log(res);
                        if (res.code == 200) {
                            //登陆成功之后将数据存储到仓库
                            this.$store.dispatch("changeUser", res.list)
                            // 登陆成功提示
                            this.$message.success(res.msg);
                            // 验证完成跳转首页
                            this.$router.push("/index")
                        } else {
                            this.$message.error(res.msg);
                        }


                    })
                }
            })
        },
        // login1(e) {
        //     if (e.keyCode === 13) {
        //         this.login()
        //     }
        // },
         //禁用浏览器返回按钮
        banBack() {
            history.pushState(null, null, document.URL);
            window.addEventListener("popstate", function () {
                history.pushState(null, null, document.URL);
            });
        }
    },
    created() {
        // window.addEventListener("keydown", this.login1, true)
        this.banBack();

    },
    watch: {},
    computed: {},
    filters: {},
    components: {}
}
</script>
<style scoped lang="less">
.wrap {
    width: 100vw;
    height: 100vh;
    position: relative;
    background-image: url(../assets/img/1.jpg);
    background-size: 100% 100%;


    .login {
        position: absolute;
        width: 450px;
        height: 250px;
        left: 77%;
        top: 50%;
        margin: -175px 0 0 -225px;
        background-color: rgba(255, 255, 255, 0.4);
        padding: 20px;
    }

    .btn {
        background-color: #e09f6a;
        color: #fff;
        border: 0;
        position: absolute;
        left: 50%;
        width: 150px;
        margin-left: -75px;

    }
}
</style>
